/*
 * Created on 2005-7-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sohu.sip.auth.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sohu.peak.util.Logger;
import com.sohu.sip.auth.business.bean.ItemBean;
import com.sohu.sip.auth.business.event.ItemAddEvent;
import com.sohu.sip.auth.business.event.ItemDeleteEvent;
import com.sohu.sip.auth.business.event.ItemDetailEvent;
import com.sohu.sip.auth.business.event.ItemListEvent;
import com.sohu.sip.auth.business.event.ItemUpdateEvent;
import com.sohu.sip.auth.business.result.ItemAddResult;
import com.sohu.sip.auth.business.result.ItemDeleteResult;
import com.sohu.sip.auth.business.result.ItemDetailResult;
import com.sohu.sip.auth.business.result.ItemListResult;
import com.sohu.sip.auth.business.result.ItemUpdateResult;
import com.sohu.sip.auth.constant.AuthConstant;
import com.sohu.sip.auth.util.ParamUtil;

/**
 * @author luciali
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AuthItemAction extends BaseAction {
	private static Logger log = new Logger();

	public String addItem() {
		return "addItem";
	}

	public String listItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			ItemListEvent event = new ItemListEvent(ParamUtil.getString(request, "pattern"),
					ParamUtil.getString(request, "paramName"),
					ParamUtil.getInt(request, "pagination.pageNO", 1).intValue(),
					ParamUtil.getInt(request, "pageCount", AuthConstant.COUNT_PERPAGE).intValue());
			ItemListResult result = ItemBean.getInstance().list(event);
			request.setAttribute("result", result);
			request.setAttribute("pagination", result.getPagination());
			if (result.getStatus() == 0)
				return "listItem";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e);
			return "failure";
		}
	}

	public String insertItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ItemAddEvent event = new ItemAddEvent(ParamUtil.getString(request, "pattern"),
					ParamUtil.getString(request, "paramName"),
					ParamUtil.getString(request, "paramValue"),
					ParamUtil.getInt(request, "priority").intValue(),
					ParamUtil.getString(request, "desc"));
			ItemAddResult result = ItemBean.getInstance().add(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				response.sendRedirect("authitem_success.sip");
				return null;
			} else {
				return "failure";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
	}

	public String modifyItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			ItemDetailEvent event = new ItemDetailEvent(ParamUtil.getLong(request, "id"));
			ItemDetailResult result = ItemBean.getInstance().getDetail(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0)
				return "modifyItem";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e.getMessage());
			return "failure";
		}
	}
	
	public String updateItem() {
		HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
		ItemUpdateEvent event = new ItemUpdateEvent(ParamUtil.getLong(request, "id"),
				ParamUtil.getString(request, "pattern"),
				ParamUtil.getString(request, "paramName"),
				ParamUtil.getString(request, "paramValue"),
				ParamUtil.getInt(request, "priority").intValue(),
				ParamUtil.getString(request, "desc"));
		ItemUpdateResult result = ItemBean.getInstance().update(event);
		request.setAttribute("result", result);
		if (result.getStatus() == 0) return "success";
		else return "failure";
	}

	public String deleteItem() {
		try {
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			HttpServletResponse response=org.apache.struts2.ServletActionContext.getResponse();
			ItemDeleteEvent event = new ItemDeleteEvent(ParamUtil.getLong(request, "id"));
			ItemDeleteResult result = ItemBean.getInstance().delete(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				response.sendRedirect("authitem_success.sip");
				return null;
			} else {
				return "failure";
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return "failure";
		}
	}

	public String success() {
		return "success";
	}

	public String failure() {
		return "failure";
	}

}
